<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>{{ titleText }}</span>
          </div>
          <div class="save_button">
            <div v-if="userId === 308" class="buttonBox">
              <el-button type="danger" icon="el-icon-close" size="mini" @click="changeStatus">？</el-button>
            </div>
            <div v-if="needMonitorHandle" class="buttonBox">
              <el-button type="danger" icon="el-icon-close" size="mini" @click="monitorBackHandle">班长退回</el-button>
              <el-button type="success" icon="el-icon-circle-check" size="mini" @click="monitorConfirmHandle">班长确认</el-button>
            </div>
            <div v-if="needEngineerHandle" class="buttonBox">
              <el-button type="danger" icon="el-icon-close" size="mini" @click="engineerBackHandle">工程师退回</el-button>
              <el-button type="success" icon="el-icon-circle-check" size="mini" @click="engineerConfirmHandle">工程师确认</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="4">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>设备编码</div>
                  <el-input v-model="data.equipments.coding" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>设备名称</div>
                  <el-input v-model="data.equipments.name" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col v-if="data.equipments.admins" :span="4">
                <el-form-item>
                  <div>操作者</div>
                  <el-input v-model="data.equipments.admins.name" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>车间</div>
                  <el-input v-model="data.workshops.name" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>生产线</div>
                  <el-input v-model="data.lines.name" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>工位</div>
                  <el-input v-model="data.processes.name" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>周期</div>
                  <el-input v-model="data.period" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="period_unit">
                  <div>周期单位</div>
                  <el-select v-model="data.period_unit" size="mini">
                    <el-option v-for="(item, periodUnitIndex) in plansPeriodUnit" :key="periodUnitIndex" :value="item.value" :label="item.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="needExpectManHours" :span="4">
                <el-form-item prop="expect_man_minutes">
                  <div>预计工时（分钟）</div>
                  <el-input v-model="data.expect_man_minutes" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="task_long">
                  <div>任务时长（小时）</div>
                  <el-input v-model="data.task_long" size="mini" />
                </el-form-item>
              </el-col>
              <el-col v-if="needMonitor" :span="4">
                <el-form-item prop="monitor">
                  <div>班长审核</div>
                  <el-select v-model="data.monitor" size="mini">
                    <el-option v-for="(item, monitorIndex) in plansCheck" :key="monitorIndex" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-if="needEngineer" :span="4">
                <el-form-item prop="engineer">
                  <div>工程师审核</div>
                  <el-select v-model="data.engineer" size="mini">
                    <el-option v-for="(item, engineerIndex) in plansCheck" :key="engineerIndex" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="halt">
                  <div>停 机</div>
                  <el-select v-model="data.halt" size="mini">
                    <el-option v-for="(item, haltIndex) in plansHalt" :key="haltIndex" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="profession">
                  <div>专业</div>
                  <el-select v-model="data.profession" size="mini">
                    <el-option v-for="(item, professionIndex) in plansProfession" :key="professionIndex" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="admin_id">
                  <div>负责人</div>
                  <el-input v-model="data.admins.name" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="arrange_time">
                  <div>安排时间</div>
                  <el-date-picker v-model="data.arrange_time" readonly type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>截止时间</div>
                  <el-date-picker v-model="data.end_time" readonly type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                </el-form-item>
              </el-col>
              <el-col v-if="data.delay" :span="4">
                <el-form-item>
                  <div>延期（天）</div>
                  <el-input v-model="data.delay" readonly size="mini" />
                </el-form-item>
              </el-col>
              <el-col v-if="needOil" :span="4">
                <el-form-item prop="lub_type">
                  <div>加油/换油</div>
                  <el-select v-model="data.lub_type" size="mini">
                    <el-option v-for="(item, lubIndex) in plansLubType" :key="lubIndex" :label="item.name" :value="item.value" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="4">
                <el-form-item>
                  <div>状态</div>
                  <el-input v-model="data.status_out" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col v-if="data.qrcode_time" :span="4">
                <div class="mb10">首次扫码时间</div>
                <el-input v-model="data.qrcode_time" size="mini" readonly />
              </el-col>
              <el-col v-if="data.submit_complate" :span="4">
                <div class="mb10">完成时间</div>
                <el-input v-model="data.submit_complate" size="mini" readonly />
              </el-col>
              <el-col v-if="data.sc" :span="4">
                <div class="mb10">执行时长</div>
                <el-input v-model="data.sc" size="mini" readonly />
              </el-col>
              <el-col v-if="data.monitos" :span="4">
                <div class="mb10">班长</div>
                <el-input v-model="data.monitos.name" size="mini" readonly />
              </el-col>
              <el-col v-if="data.monitos_date" :span="4">
                <div class="mb10">班长确认时间</div>
                <el-input v-model="data.monitos_date" size="mini" readonly />
              </el-col>
              <el-col v-if="data.engineers" :span="4">
                <div class="mb10">工程师</div>
                <el-input v-model="data.engineers.name" size="mini" readonly />
              </el-col>
              <el-col v-if="data.engineers_date" :span="4">
                <div class="mb10">工程师确认时间</div>
                <el-input v-model="data.engineers_date" size="mini" readonly />
              </el-col>
            </el-row>
            <el-row v-if="userId === 308" :gutter="10">
              <el-col :span="6">
                <el-form-item>
                  <div>扫码时间</div>
                  <el-date-picker v-model="qrcode_time" type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" @change="equalSubmitComplete" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <div>完成时间</div>
                  <el-date-picker v-model="submit_complete" type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <div>班长确认时间</div>
                  <el-date-picker v-model="monitor_date" type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <div>工程师确认时间</div>
                  <el-date-picker v-model="engineer_date" type="datetime" placeholder="选择日期时间" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="userId === 308">
              <el-col :span="6">
                <el-button type="danger" icon="el-icon-close" size="mini" @click="changeStatus">？</el-button>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="详情">
              <template>
                <el-table :data="tasksList">
                  <el-table-column label="部位" min-width="8%" prop="part" />
                  <el-table-column label="项目内容" min-width="33%" prop="param" />
                  <el-table-column label="判定标准" min-width="17%" prop="param_detail" />
                  <el-table-column label="方法" min-width="9%" prop="tasks.name" />
                  <el-table-column label="测量单位" min-width="8%" prop="task_unit" />
                  <el-table-column label="上限值" min-width="9%" prop="max" />
                  <el-table-column label="下限值" min-width="8%" prop="min" />
                  <el-table-column label="读数" min-width="15%">
                    <template scope="scope">
                      <div v-if="scope.row.tasks.type_id === 1">
                        <el-radio v-model="scope.row.reading" label="1">正常</el-radio>
                        <el-radio v-model="scope.row.reading" label="2">不正常</el-radio>
                      </div>
                      <div v-else>
                        <el-input v-model="scope.row.reading" size="mini" />
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column label="执行者" min-width="10%" prop="admins.name" />
                  <el-table-column label="执行时间" min-width="15%" prop="update_time" />
                </el-table>
                <el-button v-if="data.status === 1" type="primary" icon="el-icon-circle-check" size="mini" class="submit-reading" @click="submitReading">提交</el-button>
              </template>
            </el-tab-pane>
            <el-tab-pane v-if="needEngineer" label="时间">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="timeList" size="medium">
                      <el-table-column label="开始时间" prop="start_time" min-width="46%" />
                      <el-table-column label="结束时间" prop="end_time" min-width="46%" />
                      <el-table-column label="操作" min-width="8%" align="center">
                        <template slot-scope="{row}">
                          <div v-if="[1, 100].includes(data.status)">
                            <span class="el-tag el-tag--danger el-tag--mini cur" @click="removeTimeHandle(row.id)">
                              删除
                            </span>
                          </div>
                          <div v-else>-</div>
                        </template>
                      </el-table-column>
                      <div v-if="[1, 100].includes(data.status)" slot="append" class="pt20">
                        <el-form ref="append_time" :model="timeData" inline class="hf_form append_other_time" :rules="append_time">
                          <el-row :gutter="10">
                            <el-col :span="11">
                              <el-form-item prop="start_time">
                                <el-date-picker v-model="timeData.start_time" type="datetime" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="11">
                              <el-form-item prop="end_time">
                                <el-date-picker v-model="timeData.end_time" type="datetime" align="right" value-format="yyyy-MM-dd HH:mm:ss" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2" align="center">
                              <el-form-item>
                                <el-button type="primary" icon="el-icon-upload2" size="mini" @click="appendTimeHandle" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
              </template>
            </el-tab-pane>
            <el-tab-pane label="执行者">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="memberList">
                      <el-table-column label="偏好的员工" prop="admins.name" min-width="92%" />
                      <el-table-column label="操作" min-width="8%">
                        <template scope="scope">
                          <div v-if="[1, 100].includes(data.status)">
                            <div v-if="isAdmin">
                              <el-button type="text" size="small" @click="removeMember(scope.row.id)">删除</el-button>
                            </div>
                          </div>
                          <div v-else>
                            <span>-</span>
                          </div>
                        </template>
                      </el-table-column>
                      <div v-if="ableChangeStatusList.includes(data.status)" slot="append">
                        <el-form ref="append_member" :model="memberData" inline class="hf_form append_other_admin" :rules="append_member">
                          <el-row :gutter="10">
                            <el-col :span="22">
                              <el-form-item prop="admin_id">
                                <v-select-s v-model="memberData.admin_id" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2">
                              <el-form-item>
                                <el-button icon="el-icon-upload2" size="mini" @click="appendMember(2, 'append_member')" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
              </template>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row>
                <el-col :span="24">
                  <Tinymce id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane v-if="(needEngineer && data.admin_id === userId) || userId === 1" label="延期">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="delayList">
                      <el-table-column label="延期时长（天）" prop="day" min-width="15%" />
                      <el-table-column label="原因" prop="reason" min-width="62%" />
                      <el-table-column label="状态" min-width="15%">
                        <template slot-scope="{row}">
                          <div v-if="row.status === 1">待班长审核</div>
                          <div v-if="row.status === 2">待工程师审核</div>
                          <div v-if="row.status === 3">待科长审核</div>
                          <div v-if="row.status === 4">完成</div>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作" min-width="8%">
                        <template scope="scope">
                          <!--待办时可以点申请，如果超时需要找管理员申请，管理员可以在待办或超时状态申请
                            如果申请通过后，列表页应显示 待办（延期），更新任务时长
                            申请（班长，工程师，科长）
                            -->
                          <div v-if="userScope === 2 || isAdmin">
                            <div v-if="scope.row.status === 1">
                              <el-button type="text" size="small" @click="reviewDelayHandle(scope.row.id, 2)">班长审批</el-button>
                            </div>
                          </div>
                          <div v-if="scope.row.status === 2 && (engineerArray.includes(userId) || isAdmin)">
                            <el-button type="text" size="small" @click="reviewDelayHandle(scope.row.id, 3)">工程师审批</el-button>
                          </div>
                          <div v-if="userScope === 5 || isAdmin">
                            <div v-if="scope.row.status === 3">
                              <el-button type="text" size="small" @click="reviewDelayHandle(scope.row.id, 4)">科长审批</el-button>
                            </div>
                          </div>
                          <div v-if="scope.row.status === 1 || userScope === 1">
                            <el-button type="text" size="small" @click="removeDelayHandle(scope.row.id)">删除</el-button>
                          </div>
                          <div v-else>-</div>
                        </template>
                      </el-table-column>
                      <div v-if="userScope === 1" slot="append">
                        <el-form ref="append_delay" :model="delayData" inline class="hf_form append_other_admin" :rules="append_delay">
                          <el-row :gutter="10">
                            <el-col :span="3">
                              <el-form-item prop="day">
                                <el-input-number v-model="delayData.day" :min="1" :max="180" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="19">
                              <el-form-item prop="reason">
                                <el-input v-model="delayData.reason" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2">
                              <el-form-item>
                                <el-button icon="el-icon-upload2" size="mini" @click="appendDelayHandle" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
              </template>
            </el-tab-pane>
            <el-tab-pane v-if="needEngineer" label="工程师">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="engineerList">
                      <el-table-column label="工程师" prop="admins.name" min-width="92%" />
                      <el-table-column label="操作" min-width="8%">
                        <template scope="scope">
                          <div v-if="ableChangeStatusList.includes(data.status) && (isAdmin || userId === 1)">
                            <el-button type="text" size="small" @click="removeMember(scope.row.id)">删除</el-button>
                          </div>
                          <div v-else>
                            <span>-</span>
                          </div>
                        </template>
                      </el-table-column>
                      <div v-if="ableChangeStatusList.includes(data.status)" slot="append">
                        <el-form ref="append_engineer" :model="engineerData" inline class="hf_form append_other_admin" :rules="append_engineer">
                          <el-row :gutter="10">
                            <el-col :span="22">
                              <el-form-item prop="admin_id">
                                <v-select-s v-model="engineerData.admin_id" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2">
                              <el-form-item>
                                <el-button icon="el-icon-upload2" size="mini" @click="appendMember(1, 'append_engineer')" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
              </template>
            </el-tab-pane>
            <el-tab-pane v-if="needEngineer" label="备件">
              <template>
                <el-row>
                  <el-col :span="24">
                    <el-table :data="parts.data">
                      <el-table-column label="备品备件" prop="parts.name" min-width="46%">
                        <template scope="scope">
                          <span v-if="scope.row.already === 2" style="color: green">{{ scope.row.parts.name }}-{{ scope.row.parts.spec }}</span>
                          <span v-else-if="scope.row.already === 1" style="color: blue">{{ scope.row.parts.name }}-{{ scope.row.parts.spec }}</span>
                          <span v-else>{{ scope.row.parts.name }}-{{ scope.row.parts.spec }}</span>
                        </template>
                      </el-table-column>
                      <el-table-column label="数量" prop="number" min-width="46%">
                        <template slot-scope="scope">
                          <span v-if="scope.row.isSet">
                            <el-input v-model="editNumber" size="mini" placeholder="请输入数量" />
                          </span>
                          <span v-else>
                            {{ scope.row.number }}
                          </span>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作" min-width="8%">
                        <template v-slot="scope">
                          <div v-if="isAdmin || userId === 1">
                            <div v-if="data.status === -1 && scope.row.already === 0">
                              <span class="el-tag el-tag--info el-tag--mini cur" @click="editOrCancelPartsHandle(scope.row,scope.$index,true)">
                                {{ scope.row.isSet?'保存':"修改" }}
                              </span>
                              <span v-if="!scope.row.isSet" class="el-tag el-tag--danger el-tag--mini cur" @click="removePartsHandle(scope.row.id)">
                                删除
                              </span>
                              <span v-else class="el-tag el-tag--mini cur" @click="editOrCancelPartsHandle(scope.row,scope.$index,false)">
                                取消
                              </span>
                            </div>
                            <div v-else>-</div>
                          </div>
                          <div v-else>
                            <span>-</span>
                          </div>
                        </template>
                      </el-table-column>
                      <div v-if="[1, 20, 30, 100, 2000, 3000].includes(data.status)" slot="append">
                        <el-form ref="append_parts" :model="partsData" inline class="hf_form append_other_admin" :rules="append_parts">
                          <el-row :gutter="10">
                            <el-col :span="11">
                              <el-form-item prop="part_id">
                                <v-select-s v-model="partsData.part_id" :query="{add_field: 'spec'}" action="partsList" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="11">
                              <el-form-item prop="number">
                                <el-input v-model="partsData.number" size="mini" />
                              </el-form-item>
                            </el-col>
                            <el-col :span="2">
                              <el-form-item>
                                <el-button icon="el-icon-upload2" size="mini" @click="appendParts" />
                              </el-form-item>
                            </el-col>
                          </el-row>
                        </el-form>
                      </div>
                    </el-table>
                  </el-col>
                </el-row>
                <el-row v-if="data.status === 0 && parts.data.length > 0">
                  <el-button type="primary" size="mini" class="submit-reading" @click="getParts">请求</el-button>
                </el-row>
              </template>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/workorder/pm'
import { mapGetters } from 'vuex'
import { plansProfession, plansPeriodUnit, plansCheck, plansHalt, plansLubType, pmStatus } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
export default {
  inject: ['backUrl', 'addOne', 'goUrl', 'delOne', 'update', 'reload', 'getScope', 'changePmStatus', 'selectName'],
  components: {
    Tinymce,
    VSelect,
    VSelectS
  },
  data() {
    return {
      host: 'https:router.ubeiyun.com/',
      num: 0,
      data: {
        admins: {
          name: ''
        },
        cr_admins: {
          name: ''
        },
        equipments: {
          coding: '',
          name: ''
        },
        workshops: {
          name: ''
        },
        lines: {
          name: ''
        },
        processes: {
          name: ''
        },
        monitors: {},
        engineers: {}
      },
      parts: {
        sel: null,
        data: []
      },
      partsEditId: 1,
      userScope: 1,
      isAdmin: false,
      // 下面的选项卡
      tasksList: [],
      delayList: [],
      engineerList: [],
      memberList: [],
      timeList: [],
      append_delay: {
        day: [
          {
            required: true,
            message: '延期时长',
            trigger: 'change'
          }
        ],
        reason: [
          {
            required: true,
            message: '原因',
            trigger: 'change'
          }
        ]
      },
      append_member: {
        admin_id: [
          {
            required: true,
            message: '请选择人员',
            trigger: 'change'
          }
        ]
      },
      append_engineer: {
        admin_id: [
          {
            required: true,
            message: '请选择工程师',
            trigger: 'change'
          }
        ]
      },
      append_parts: {
        parts_id: [
          {
            required: true,
            message: '请选择备件',
            trigger: 'change'
          }
        ],
        number: [
          {
            required: true,
            message: '请填写数量',
            trigger: 'change'
          }
        ]
      },
      append_time: {
        start_time: [
          {
            required: true,
            message: '开始时间',
            trigger: 'change'
          }
        ],
        end_time: [
          {
            required: true,
            message: '结束时间',
            trigger: 'change'
          }
        ]
      },
      engineerArray: [],
      delayData: {},
      engineerData: {},
      memberData: {},
      partsData: {},
      timeData: {},
      qrcode_time: '',
      submit_complete: '',
      monitor_date: '',
      engineer_date: '',
      loading: false,
      titleText: '',
      needParts: false,
      needMonitor: false,
      needEngineer: false,
      needOil: false,
      needExpectManHours: false,
      needMonitorHandle: false,
      needEngineerHandle: false,
      category_id: '',
      ableChangeStatusList: [1, 20, 30, 100, 2000, 3000], // 还可以操作的状态（比如添加人员）
      plansProfession,
      plansPeriodUnit,
      plansCheck,
      plansHalt,
      plansLubType,
      pmStatus
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    this.index()
  },
  methods: {
    index() {
      this.category_id = this.$route.path.split('/')[2].split('pm')[1].split('_edit')[0]
      const titleArray = ['', '自主点检', '自主维护', '专业维护', '加油润滑', '专业点检']
      this.titleText = titleArray[this.category_id]

      this.info()
      // 2自主维护 3专业维护 4润滑 6专业巡检
      const needParts = ['3', '4']
      const needMonitor = ['3', '4']
      const needEngineer = ['3', '4']
      const needOil = ['4']
      const needExpectManHours = ['2', '3', '4']

      this.needParts = needParts.includes(this.category_id)
      this.needMonitor = needMonitor.includes(this.category_id)
      this.needEngineer = needEngineer.includes(this.category_id)
      this.needOil = needOil.includes(this.category_id)
      this.needExpectManHours = needExpectManHours.includes(this.category_id)
    },
    info() {
      const id = this.$route.params.id
      API.getOne(id).then(response => {
        if (response.data.error_code) {
          this.$alert(response.data.message, '错误', {
            confirmButtonText: '确定',
            callback: () => {
              this.backUrl()
            }
          })
        }
        this.data = response.data
        this.data.status_out = this.selectName(this.pmStatus, this.data.status)
        // 上面那四个审核按钮是否显示
        if (this.needMonitor && (this.data.status === 20 || this.data.status === 2000)) {
          this.needMonitorHandle = true
        }

        if (this.needEngineer && (this.data.status === 30 || this.data.status === 3000)) {
          this.needEngineerHandle = true
        }
        // 此账号的管理权限
        this.userScope = this.getScope(this.userId)
        if (this.userScope === 1 || this.userId === response.data.admin_id) {
          this.isAdmin = true
        }
        // 下面的tabs
        this.tasksList = this.data.other_tasks
        this.memberList = this.data.other_members
        this.engineerList = this.data.other_engineers
        this.delayList = this.data.other_delay
        this.timeList = this.data.other_times

        this.engineerArray = this.memberList.map(item => {
          return item.admin_id
        })

        // used by jhb
        if (this.data.qrcode_time) {
          this.qrcode_time = this.data.qrcode_time
          this.submit_complete = this.data.submit_complate
        }

        if (this.data.wxBz_date) {
          this.monitor_date = this.data.wxBz_date
        }

        if (this.data.engineer_date) {
          this.engineer_date = this.data.engineer_date
        }
      })
    },
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    appendParts() {
      this.$refs.append_parts.validate((valid) => {
        if (valid) {
          const part_id = this.partsData.part_id
          const number = this.partsData.number
          const parts_data = {
            'parts_id': part_id,
            'number': number,
            'work_order_id': this.$route.params.id
          }
          API.appendParts(parts_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(this.$route.params.id).then(response => {
                this.parts.data = response.data.other_parts
              })
            } else if (response.data.error_code === 62004) {
              this.$message.error('该备件已存在')
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removePartsHandle(id) {
      const parts_data = {
        'id': id
      }
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.removeParts(parts_data).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            API.getOne(this.$route.params.id).then(response => {
              this.parts.data = response.data.other_parts
            })
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editOrCancelPartsHandle(row, index, cg) {
      // 点击修改 判断是否已经保存所有操作
      for (const i of this.parts.data) {
        if (i.isSet && i.id !== row.id) {
          this.$message.warning('请先保存当前编辑项')
          return false
        }
      }
      // 是否是取消操作
      if (!cg) {
        if (!this.parts.sel.id) {
          this.parts.data.splice(index, 1)
        }
        return this.$set(this.parts.data[index], 'isSet', false)
      }
      // 提交数据
      const _this = this
      if (row.isSet) {
        (function() {
          const edit_parts_data = {
            'number': _this.editNumber,
            'id': _this.partsEditId
          }
          API.editParts(edit_parts_data).then(response => {
            if (response.data.error_code === 1) {
              _this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              API.getOne(_this.$route.params.id).then((res) => {
                _this.parts.data = res.data.other_parts
              })
            } else {
              this.$message.error(response.data.message)
            }
          })
        })()
      } else {
        this.parts.sel = JSON.parse(JSON.stringify(row))
        this.$set(this.parts.data[index], 'isSet', true)
        this.editNumber = this.parts.data[index].number
        this.partsEditId = this.parts.data[index].id
      }
    },
    appendMember(engineer, form) {
      const admin_id = form === 'append_member' ? this.memberData.admin_id : this.engineerData.admin_id
      this.$refs[form].validate((valid) => {
        if (valid) {
          API.appendMember({
            engineer,
            admin_id,
            workorder_id: this.$route.params.id,
            plan_category: this.category_id
          }).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，更新成功',
                type: 'success'
              })
              this.info()
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removeMember(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.removeMember({ id }).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            this.info()
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getParts() {
      const needData = {
        id: this.data.id
      }
      API.needParts(needData).then(response => {
        if (response.data.error_code === 1) {
          this.$message.success('请求备件，请速去库房领件！')
          this.backUrl()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    submitReading() {
      const postData = {
        data: this.tasksList,
        id: this.$route.params.id,
        eq: this.data.equipment_id
      }
      API.submitReading(postData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.goBack()
        } else if (response.data.error_code === 11) {
          this.$message({
            message: '参数不正常，已自动生成报修单',
            type: 'success'
          })
          this.goBack()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    finishHandle() {
      const reviewData = {
        'status': 1,
        'content': this.data.content,
        'id': this.$route.params.id
      }
      API.review(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，提交成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    monitorBackHandle() {},
    monitorConfirmHandle() {},
    engineerBackHandle() {},
    engineerConfirmHandle() {},
    goBack() {
      this.goUrl(`/workorder/pm${this.category_id}`)
    },
    appendDelayHandle() {
      this.$refs.append_delay.validate((valid) => {
        if (valid) {
          const day = this.delayData.day
          const reason = this.delayData.reason
          const delay_data = {
            day,
            reason,
            admin_id: this.userId,
            workorder_id: this.$route.params.id
          }
          API.appendDelay(delay_data).then(response => {
            if (response.data.error_code === 1) {
              this.$message({
                message: '恭喜你，提交成功',
                type: 'success'
              })
              this.info()
            } else {
              this.$message.error(response.data.message)
            }
          })
        }
      })
    },
    removeDelayHandle(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        API.removeDelay({ id }).then((response) => {
          if (response.data.error_code === 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
            this.info()
          } else {
            this.$message.error(response.data.message)
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    reviewDelayHandle(id, status) {
      const reviewData = {
        id,
        status,
        workorder_id: this.$route.params.id
      }
      API.reviewDelay(reviewData).then((response) => {
        if (response.data.error_code === 1) {
          this.$message({
            message: '恭喜你，审核成功',
            type: 'success'
          })
          this.reload()
        } else {
          this.$message.error(response.data.message)
        }
      })
    },
    appendTimeHandle() {
      this.$refs.append_time.validate((valid) => {
        if (valid) {
          const postData = this.timeData
          postData.workorder_id = this.data.id
          postData.equipment_id = this.data.equipment_id
          postData.plan_category = this.category_id
          API.appendTime(postData).then(res => {
            if (res.data.error_code === 1) {
              this.$message({
                message: '恭喜你，添加成功',
                type: 'success'
              })
              this.info()
            } else {
              this.$message.error(res.data.message)
            }
          })
        }
      })
    },
    removeTimeHandle(id) {
      const data = {
        id,
        plan_category: this.category_id
      }
      API.removeTime(data).then(res => {
        if (res.data.error_code === 1) {
          this.$message.success('删除成功')
          this.info()
        }
      })
    },
    /*
    * used by jhb
    * */
    changeStatus() {
      if (!this.qrcode_time) {
        this.$message.error('先填写时间')
        if (!this.submit_complate) {
          this.submit_complate = this.qrcode_time
        }
        return false
      } else {
        const data = {
          id: this.data.id,
          submit_complate: this.submit_complate,
          qrcode_time: this.qrcode_time,
          wxBz_date: this.wxBz_date,
          engineer_date: this.engineer_date,
          pm: 'pm3'
        }
        API.jhb(data).then(res => {
          if (res.data.error_code === 1) {
            this.$message.success('成功')
            this.reload()
          } else {
            this.$message.error(res.data.message)
          }
        })
      }
    },
    equalSubmitComplete() {
      this.submit_complate = this.qrcode_time
    }
  }
}
</script>

<style>
.submit-reading{margin: 10px 0 0 10px;}
</style>
